//
//  CYSMSExpireManager.m
//  cyjf
//
//  Created by tauCross on 2017/5/4.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYSMSExpireManager.h"



#define CYSMSExpireUserDefaultsKey @"SMSExpire"



@interface CYSMSExpireManager ()

@property(nonatomic, strong)NSMutableDictionary *smsExpireDict;

@end


@implementation CYSMSExpireManager

+ (instancetype)standardManager
{
    static CYSMSExpireManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[CYSMSExpireManager alloc] init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if(self)
    {
        NSDictionary *dict = [[NSUserDefaults standardUserDefaults] objectForKey:CYSMSExpireUserDefaultsKey];
        if(dict)
        {
            self.smsExpireDict = [NSMutableDictionary dictionaryWithDictionary:dict];
        }
        else
        {
            self.smsExpireDict = [NSMutableDictionary dictionary];
        }
    }
    return self;
}

- (void)resetExpireDate:(NSDate *)date type:(CYSMSExpireType)type
{
    [self.smsExpireDict setObject:date forKey:@(type).stringValue];
    [[NSUserDefaults standardUserDefaults] setObject:self.smsExpireDict forKey:CYSMSExpireUserDefaultsKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSTimeInterval)secondsWithType:(CYSMSExpireType)type
{
    NSDate *date = self.smsExpireDict[@(type).stringValue];
    if(date)
    {
        return ceil([date timeIntervalSinceNow]);
    }
    return 0;
}

@end
